package org.omich.loaves;

public class Loaf {
	public Loaf(int weight) {
		this.mWeight = weight;
	} 
	public String GetEater() { return mEater; }
	public int getWeight() { return mWeight; }
	public int getTail() { 
		long currentTime = System.currentTimeMillis();
		long tail = mWeight - (currentTime - mStartEaten);
		if (tail < 0)
			return 0;
		return (int)(tail); }
	
	public boolean isEaten() {
		return !(isFree() || isEating());
	}
	
	public boolean isEating() {
		if (isFree()) {
			return false;
		}
		if (System.currentTimeMillis() <= mStartEaten + mWeight)
			return true;
		return false;
	}
	
	public boolean isFree() { return mStartEaten == 0; }
	
	public boolean startEating(String eater) {
		if (!isFree())
			return false;
		mStartEaten = System.currentTimeMillis();
		this.mEater = eater;
		return true;
	}
	
	private int mWeight;
	private long mStartEaten = 0;
	private String mEater;
}
